package com.example.hrdemo.api.controller;

import com.example.hrdemo.api.domain.UserAllVo;
import com.example.hrdemo.api.domain.UserAllPageVo;
import com.example.hrdemo.api.domain.UserInfoVo;
import com.example.hrdemo.module.entity.User;
import com.example.hrdemo.module.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    //测试
    @RequestMapping("/user/test")
    public User userTest(@RequestParam(name = "userId") BigInteger userId){
        return userService.getUserInfo(userId);
    }

    //列表
    @RequestMapping("/user/all")
    public UserAllPageVo userAllPage(@RequestParam(name ="page") Integer page,@RequestParam(required = false,name ="keyword") String keyword){
        UserAllPageVo userAllPage = new UserAllPageVo();
        //设置一页显示五条
        int pageSize = 5;
        //是否结束
        Boolean isEnd = false;
        System.out.println(keyword);
        //获取列表
        List<User> userList = userService.getAllPage(keyword,page,pageSize);
        //获取列表大小
        int listSize = userList.size();
        if (listSize < pageSize){
            isEnd = true;
        } else {
            isEnd = false;
        }
        List<UserAllVo> userAllVo = new ArrayList<>();
        for (User u : userList) {
            UserAllVo userAll = new UserAllVo();
            userAll.setId(u.getId());
            //多图显示第一个
            String pictures = u.getPicturesUrl();
            String[] picture = pictures.split("\\$");
            String picture1 = picture[0].trim();
            userAll.setPicturesUrl(picture1);
            userAll.setUserName(u.getUserName());
            userAll.setUserWorkNum(u.getUserWorkNum());
            userAll.setJobName(u.getJobName());
            userAllVo.add(userAll);
        }
        userAllPage.setUserAllList(userAllVo);
        userAllPage.setIsEnd(isEnd);
        return userAllPage;
    }

//    //列表
//    @RequestMapping("/user/all")
//    public UserAllPageVo userAllPage(@RequestParam(name ="page") Integer page){
//        UserAllPageVo userAllPage = new UserAllPageVo();
//        //设置一页显示五条
//        int pageSize = 5;
//        //获取总条数
//        int total = userService.getUserCount();
//        //计算页数
//        int pages;
//        if (total%pageSize == 0){
//            pages = total/pageSize;
//        } else {
//            pages = total/pageSize+1;
//        }
//        //是否结束
//        Boolean isEnd = false;
//        //判断是否结束
//        if (page>pages){
//            isEnd = true;
//            userAllPage.setIsEnd(isEnd);
//            return userAllPage;
//        } else {
//            List<User> userList = userService.getAllPage(page,pageSize);
//            List<UserAllVo> userAllVo = new ArrayList<>();
//            for (User u : userList) {
//                UserAllVo userAll = new UserAllVo();
//                userAll.setId(u.getId());
//                //多图显示第一个
//                String pictures = u.getPicturesUrl();
//                String[] picture = pictures.split("\\$");
//                String picture1 = picture[0].trim();
//                userAll.setPicturesUrl(picture1);
//                userAll.setUserName(u.getUserName());
//                userAll.setUserWorkNum(u.getUserWorkNum());
//                userAll.setJobName(u.getJobName());
//                userAllVo.add(userAll);
//            }
//            userAllPage.setUserAllList(userAllVo);
//            if (page == pages){
//                isEnd = true;
//                userAllPage.setIsEnd(isEnd);
//            } else {
//                userAllPage.setIsEnd(isEnd);
//            }
//            return userAllPage;
//        }
//    }

    //详情
    @RequestMapping("/user/info")
    public UserInfoVo userInfo(@RequestParam(name = "userId") BigInteger userId){
        User user = userService.getUserInfo(userId);
        if (user == null){
            return null;
        } else {
            UserInfoVo userInfoVo = new UserInfoVo();
            userInfoVo.setId(user.getId());
            userInfoVo.setUserName(user.getUserName());
            userInfoVo.setUserGender(user.getUserGender());
            userInfoVo.setUserCardNum(user.getUserCardNum());
            userInfoVo.setBirthday(user.getBirthday());
            userInfoVo.setEducation(user.getEducation());
            userInfoVo.setBirthplace(user.getBirthplace());
            userInfoVo.setPhone(user.getPhone());
            userInfoVo.setUserWorkNum(user.getUserWorkNum());
            userInfoVo.setJobName(user.getJobName());
            userInfoVo.setWages(user.getWages());
            userInfoVo.setWorkDay(user.getWorkDay());
            userInfoVo.setFormalWorkDay(user.getFormalWorkDay());
            userInfoVo.setWorkAge(user.getWorkAge());

            int status = user.getUserStatus();
            Map<Integer,String> statusMap = new HashMap<>();
            statusMap.put(1,"试用");
            statusMap.put(2,"正式");
            statusMap.put(3,"离职");
            String s = statusMap.get(status);
            userInfoVo.setUserStatus(s);
            //String status = Integer.toString(user.getUserStatus());
            //        1 == status ? "试用" :(2 == status ? "正式" : "离职")  类型多了的话就不能用三目运算了

            //多图按照列表显示出来
            String pictures = user.getPicturesUrl();
            List<String> pictureList = Arrays.stream(pictures.split("\\$")).collect(Collectors.toList());
            userInfoVo.setPicturesUrl(pictureList);
            return userInfoVo;
        }
    }
}
